<script setup lang="ts">

export interface News {
  id: number;
  title: string;
  date: string;
  excerpt: string;
  img: string;
}

defineProps<{
  newsList: News[];
  loading: boolean;
  error: string;
}>();

const emit = defineEmits<{
  (e: 'add'): void;
  (e: 'edit', news: News): void;
  (e: 'delete', id: number): void;
}>();

const handleAdd = () => {
  emit('add');
};

const handleEdit = (news: News) => {
  emit('edit', news);
};

const handleDelete = (id: number) => {
  emit('delete', id);
};
</script>

<template>
  <div class="news-management">
    <h2>新闻管理</h2>
    <button @click="handleAdd" class="btn-add">添加新闻</button>
    
    <div v-if="loading">加载中...</div>
    <div v-else-if="error" class="error">{{ error }}</div>
    <div v-else class="news-list">
      <table>
        <thead>
          <tr>
            <th>标题</th>
            <th>日期</th>
            <th>摘要</th>
            <th>图片</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="news in newsList" :key="news.id">
            <td>{{ news.title }}</td>
            <td>{{ news.date }}</td>
            <td>{{ news.excerpt }}</td>
            <td>{{ news.img }}</td>
            <td>
              <button @click="handleEdit(news)" class="btn-edit">编辑</button>
              <button @click="handleDelete(news.id)" class="btn-delete">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<style scoped>
.news-management {
  margin: 20px 0;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

th, td {
  padding: 10px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

th {
  background-color: #f5f5f5;
}

.btn-add {
  background-color: #4CAF50;
  color: white;
  border: none;
  margin-bottom: 15px;
  padding: 5px 10px;
  cursor: pointer;
}

.btn-edit {
  background-color: #2196F3;
  color: white;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
  margin-right: 5px;
}

.btn-delete {
  background-color: #f44336;
  color: white;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
}

.error {
  color: #f44336;
  margin-bottom: 15px;
}
</style>